import http from '../../apis/api.js';
import util from '../../utils/util.js';
let self
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dealRepair: false,/**是否展示故障单处理 */
    addComponents: false,/**是否展示换上的零件或备件列表 */
    uninstallMaterial: false,/**是否展示换上的零件或备件列表 */
    id: '',
    detail: null,
    uninstallMaterialList:[],
    selfRepairUseList:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    self = this;
    self.data.id = options.id;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    self.getRepaitDetail();
    /**查询换上的零件或备件列表*/
    self.getSelfRepairUseList();
    /**查询换下的部件列表*/
    self.getUninstallMaterialList()
  },
  

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**获取保养单详情*/
  getRepaitDetail() {
    http.maintainDetail(self.data.id).then(res => {
      self.data.detail = res.content;
      console.log(self.data.detail);
      self.setData({
        detail: self.data.detail
      })
    })
  },
  /**获取换上的零件或备件列表*/
  getSelfRepairUseList() {
    http.maintainRepairUseList(self.data.id).then(res => {
      self.data.selfRepairUseList = res.content;
      self.setData({
        selfRepairUseList: self.data.selfRepairUseList
      })
    })
  },
  changedComponents(e) {
    /**新增换上的零件或备件列表*/
    let routerUrl = "";
    switch (e.currentTarget.dataset.type) {
      case 'change':
        routerUrl = `/pages/addComponents/addComponents?id=${self.data.id}&repairType=maintain&pid=${self.data.detail.pid}`
        break
      case 'uninstallMaterial':
        routerUrl = `/pages/addUninstallMaterial/addUninstallMaterial?id=${self.data.id}&repairType=maintain`
        break
    }
    wx.navigateTo({
      url: routerUrl,
    })

  },
  /**切换显示隐藏详情*/
  showDetail(e) {
    let opt = e.currentTarget.dataset;
    switch (opt.type) {
      case 'dealRepair':
        self.data.dealRepair = !self.data.dealRepair;
        self.setData({
          dealRepair: self.data.dealRepair
        })
        break;
      case 'addComponents':
        self.data.addComponents = !self.data.addComponents;
        self.setData({
          addComponents: self.data.addComponents
        })
        break;
      case "uninstallMaterial":
        self.data.uninstallMaterial = !self.data.uninstallMaterial;
        self.setData({
          uninstallMaterial: self.data.uninstallMaterial
        })
        break;
    }
  },
  goRouter(){
    wx.navigateTo({
      url: `/pages/addMaintain/addMaintain?id=${self.data.id}`,
    })
  },
  /**删除*/ 
  deleteRepairUse(e) {
    console.log(e);
    let id = e.currentTarget.dataset.id
    let type = e.currentTarget.dataset.type;
    switch (type) {
      case 'addComponents':
        wx.showModal({
          title: '是否移除换上的零件或备件',
          success(res) {
            if (res.confirm) {
              http.deleteMaintainRepairUse(id).then(res => {
                wx.showToast({
                  title: '移除成功',
                  icon: 'success',
                  duration: 2000,
                  success() {
                    self.getSelfRepairUseList();
                  }
                })
              })
            }
          }
        })
        break;
      case 'uninstallMaterial':
        wx.showModal({
          title: '是否移除换下的部件',
          success(res) {
            if (res.confirm) {
              http.deleteUninstallMaterial(id).then(res => {
                wx.showToast({
                  title: '移除成功',
                  icon: 'success',
                  duration: 2000,
                  success() {
                    self.getUninstallMaterialList();
                  }
                })
              })
            }
          }
        })
        break;
    }
  },
/**获取派修单换下的部件*/
  getUninstallMaterialList() {
    http.uninstallMaterialList(self.data.id, 3).then(res => {
      self.data.uninstallMaterialList = res.content;
      console.log(self.data.uninstallMaterialList);
      self.setData({
        uninstallMaterialList: self.data.uninstallMaterialList
      })
    })
  }

})